﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CMSLauncher;

namespace Update
{
    public partial class Form1 : Form
    {
        private string StatusText = "";
        private int MaxProgress = 10;
        private int LaunchStatus = 0;
        private const int THREAD_WAIT = 100;
        public Form1()
        {
            InitializeComponent();

        }

        private void timerRefresh_Tick(object sender, EventArgs e)
        {
            timerRefresh.Stop();

            labelStatus.Text = StatusText;
            if (progressBarStatus.Value + 2 <= MaxProgress)
            {
                progressBarStatus.Value += 2;
            }

            if (LaunchStatus == 1)
            {
                //调用本体程序
                Process p = new Process();
                p.StartInfo.FileName = Application.StartupPath + "\\cms.exe";
                p.Start();
                p.Close();
                LaunchStatus = 2;
            }

            if (LaunchStatus == 10)
            {
                if (Opacity > 0)
                {
                    Opacity -= 0.1;
                }
                else
                {
                    Close();
                }
            }

            timerRefresh.Start();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timerRefresh.Start();
            Thread initThread = new Thread(new ThreadStart(Init));
            initThread.Start();
        }

        //获取网络版本

        //比较是否需要更新

        //需要更新，关闭当前程序，准备更新

        //更新

        //更新完毕，打开程序

        //关闭退出
        private void Init()
        {
            //首先获取当前版本
            StatusText = "读取本地信息...";
            Thread.Sleep(THREAD_WAIT);

            int curBuildVersion = PubClass.ConvertToInt32(ConfigFile.ReadValue("Version"), -1);
            MaxProgress += progressBarStatus.Maximum / 4;
            try
            {
                string urlPath = "http://www.hotstation.space/download/version.txt";
                StatusText = "访问资源服务器 " + urlPath;
                Thread.Sleep(THREAD_WAIT);

                WebClient wc = new WebClient();
                wc.DownloadFile(urlPath, "_version.txt");

                int latestBuildVersion = PubClass.ConvertToInt32(ConfigFile.ReadValue("Client", "BuildVersion", VersionIniFile), 0);
                string[] files = ConfigFile.ReadValue("Client", "Files", VersionIniFile).Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                if (latestBuildVersion == 0)
                {
                    StatusText = "无法获取资源 " + urlVersion;
                    Thread.Sleep(THREAD_WAIT * 10);
                    continue;
                }

                foreach (string file in files)
                {
                    MaxProgress += progressBarStatus.Maximum / 4 / files.Length;

                    try
                    {
                        if (curBuildVersion != latestBuildVersion || !File.Exists(StartPath + file))
                        {
                            StatusText = "正在下载文件 " + file;
                            Thread.Sleep(THREAD_WAIT);

                            if (!Directory.Exists(Path.GetDirectoryName(StartPath + file)))
                                Directory.CreateDirectory(Path.GetDirectoryName(StartPath + file));
                            wc.DownloadFile(urlPath + file, StartPath + file);
                        }
                    }
                    catch
                    {
                        // 通常是下载文件失败
                    }
                }

                ConfigFile.WriteValue("Client", "BuildVersion", latestBuildVersion.ToString());
                StatusText = "已更新至最新版";
                Thread.Sleep(THREAD_WAIT);
                break;
            }
            catch
            {
                // 通常是无法访问到服务器
            }

            string[] checkFiles = ConfigFile.ReadValue("Client", "Files", VersionIniFile).Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string file in checkFiles)
            {
                MaxProgress += progressBarStatus.Maximum / 4 / checkFiles.Length;
                StatusText = "正在加载 " + file;
                Thread.Sleep(THREAD_WAIT);
                if (!File.Exists(StartPath + file))
                {
                    StatusText = "无法启动CMS，找不到文件 " + file;
                    LaunchStatus = -1;
                    return;
                }
            }

            MaxProgress = progressBarStatus.Maximum;
            StatusText = "正在启动CMS...";
            LaunchStatus = 1;
            Thread.Sleep(THREAD_WAIT * 20);
            LaunchStatus = 10;
        }




    }
}
